import config from '../config'
import {getToken} from './auth'
import axios from "axios";


export default class Utils{
    /**
   * 根据当前所处的开发环境，使用对应的ajax请求地址
   *
   */
  static BaseApi () {
    if (process.env.NODE_ENV == "development") {
      return config.baseUrl.dev;
    } else {
      return config.baseUrl.prod;
    }
  }
  /**
   * 请求头参数   *
   */
  static TokenAuthorization () {
    if (process.env.NODE_ENV == "development") {
      return config.baseUrl.devRefsauth;
    } else {
      return config.baseUrl.prodRefsauth;
    }
  }
  /**
   * 刷新token
   *
   */
  static TokenBaseApi () {
    if (process.env.NODE_ENV == "development") {
      return config.baseUrl.devToken;
    } else {
      return config.baseUrl.prodToken;
    }
  }
  /**
   * pms登录
   *
   */
  static XZBaseApi () {
    if (process.env.NODE_ENV == "development") {
      return config.baseUrl.devXZtoken;
    } else {
      return config.baseUrl.prodXZoken;
    }
  }
  /**
   * pms呼叫服务
   *
   */
  static XZchatBaseApi () {
    if (process.env.NODE_ENV == "development") {
      return config.baseUrl.devXzchat;
    } else {
      return config.baseUrl.proXzchat;
    }
  }
  /***
   * 导出exlce
   * @param params
   * @constructor
   */
  static ExportSign(params,url, name) {
    const axiosAjax = axios.create({
      timeout: 1000 * 60,
    });
    axiosAjax.get(`${this.BaseApi()}${url}`, params, {
      headers: {
        Authorization: getToken()
      }
    },).then((res) => {
      const blob = new Blob([res.data], {
        type: 'application/vnd.ms-excel;charset=utf-8'
      })
      const fileName = name + '.xlsx'
      if ('download' in document.createElement('a')) {
        // 非IE下载
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href)
        document.body.removeChild(elink)
      } else {

        navigator.msSaveBlob(blob, fileName)
      }
    })
  }


  /***
   * @overview 函数防抖
   * @param fn
   * @param wait
   * @param immediate
   * @returns {(function(): void)|*}
   */
  static debounce(fn, wait=500, immediate=true) {
    let timeout
    return function () {
      let th = this
      let ar = arguments
      if (timeout) clearTimeout(timeout)
      if (immediate) {
        if (!timeout) fn.apply(th, ar)
        timeout = setTimeout(() => {
          timeout = null
        }, wait)
      } else {
        timeout = setTimeout(() => {
          fn.apply(th, ar)
        }, wait)
      }
    }
  }

  /***
   * @overview 函数节流
   * @param fn
   * @param wait
   * @returns {(function(): void)|*}
   */
  static throttle(fn, wait) {
    let timeout
    return function () {
      let th = this
      let ar = arguments
      if (!timeout) {
        timeout = setTimeout(() => {
          fn.apply(th, ar)
          timeout = null
        }, wait)
      }
    }
  }
  /**
   * 10x
   * */
  static isExternal(path) {
    return /^(https?:|mailto:|tel:)/.test(path)
  }
}
